window.View = function()
{
    // 获取接口路径
    this.Paths = null;
    // 获取所有选中元素
    this.Elems = null;
    // 视图模板
    this.Tmps = null;

    this.elementItem = null;

    this.posArray = [];

    this._innerWidth = 0;

    this.elementSize = {};

    this.elementPadding = 20;
};

View.prototype = {

    /** 初始化视图*/
    init : function( o )
    {
        this.lib = o.lib || null ;
        // 获取接口路径
        this.Paths = o.paths;
        // 获取所有选中元素
        this.Elems = this.H.getElementsByIds( o.ids );
        // 获取所有视图模板
        this.Tmps = this.H.getTemplatesModle( o.templates );

        // 初始化select2
        this.$("select.ad-js-select").select2({
            placeholder: "请选择查询条件",
            allowClear: true
        });


        this.bindEvent();
    },

    _getElementsPosData : function( tag )
    {
        var array = [];

        for( var i = 0, elem; elem = this.elementItem[ i ]; i += 1 )
        {
            if( elem.style.display == "none" ) continue;

            array.push({ value : parseInt( elem.getAttribute( tag ), 10 ) || null, element : elem } );
        }

        return array;
    },

    _setPosition : function( array )
    {
        for( var i = 0, obj; obj = array[ i ]; i += 1 )
        {
            obj.element.style.top = this.posArray[ i ].y + "px";
            obj.element.style.left = this.posArray[ i ].x + "px";
        }
    },

    _operatBindEvent : function()
    {
        var self = this;

        window.addEventListener( "resize", function()
        {
            self._innerWidth = window.innerWidth;

//            self.rangeElementsHandle();

        }, false );


    },

    insertSort : function( array )
    {
        var i = 1, j, temp, value, element,
            len = array.length;

        for(;i < len; i++)
        {
            temp = j = i;

            value = array[ j ].value;

            element = array[ j ].element;

            while( --j > -1 )
            {
                if( array[ j ].value < value )
                {
                    array[ j + 1 ].value = array[ j ].value;

                    array[ j + 1 ].element = array[ j ].element;
                }
                else
                {
                    break;
                }
            }

            array[ j + 1 ].value = value;

            array[ j + 1 ].element = element;
        }

        return array;

    },

    _mainItemBindEvent : function()
    {
        var self = this;

        self.$( self.Elems["listItem"]).delegate( "li", "click", function( e )
        {
            var _this = this;

            if( _this.getAttribute("data-href") )
            {
                if( e.target.nodeName == "BUTTON" )
                {
                    if( confirm("系统提示：是否删除分析模型？") )
                    {
                        self.deleteItem( e.target.getAttribute("data-href"), _this, function()
                        {
                            self.Elems["rangeItem"].onchange();
                        });
                    }
                }
                else
                {
                    self.beforeSend();

                    var ofdetails = top.window.FRAMES[ "details" ].element;

                    ofdetails.onload = function()
                    {
                        var fthis = this;

                        clearTimeout( self.dTimer );

                        self.dTimer = setTimeout( function()
                        {
                            fthis.style.opacity = 1;

                            fthis.style.zIndex = 10;

                        }, 100 );

                        //self.afterSend();
                    };

                    ofdetails.src = _this.getAttribute("data-href") + "?t=" + new Date().getTime();
                }

            }
            else if( _this.getAttribute("data-create") )
            {
                self.$( "a:eq(1)" , top.document.getElementById("js-nav") ).click().mouseover();
            }
            else
            {
                self.toastr.warning("敬请期待");
            }
        });
    },

    bindEvent : function()
    {
        this._mainItemBindEvent();

        this._operatBindEvent();

        var self = this;

        self.$(".ad-doc-dl").on("click","dt",function(){
           $(this).parent().toggleClass("active");
        });

        self.$(".ad-doc-dl").on("click","dd",function(){
            self.$(".ad-doc-dl dd").removeClass("active");
            self.$(this).addClass("active");
        }).on("click","dd>a",function( ev ){
            var oLink = $(this).attr("href");
            Handle&Handle.loadingShow();
        });

        self.$( function (){
            var  oDl =self.$(".ad-doc-dl dd:first");
            var lSrc = oDl.find("a").attr("href");
            oDl.addClass("active");

            $("#detail-frame").attr("src" , lSrc )
        });

    },

    /** 请求前处理*/
    beforeSend : function()
    {
        var oloading = this.Elems["loading"];

        oloading.style.display = "block";
        oloading.style.opacity = 1;
        oloading.style["-webkit-transition"] =
            oloading.style["-moz-transition"] =
                oloading.style["-o-transition"] =
                    oloading.style["transition"] = "opacity 0.35s ease";
    },

    afterSend : function()
    {
        var oloading = this.Elems["loading"];

        oloading.style.opacity = 0;
        oloading.style.display = "none";
    },

    deleteSuccess : function( item, cplt, cb, options )
    {
        if( item )
        {
            this.$( options.element).remove();
        }
        else
        {
            this.toastr.warning("删除模块未成功");
        }

        if( cb ) cb.call( this, item );

        this.afterSend();
    },

    /** 土地数据查询*/
    deleteItem : function( url, self, callback )
    {
        var callbackObject = {
            context : this,
            success: this.deleteSuccess || function(){},
            beforeSend : this.beforeSend || function(){},
            callback : callback || function(){},
            options : {
                element : self
            }
        };

        this.Control.post( url, null, callbackObject, "json"  )
    }

};


